"""
@author：yiwang
@file_name: __init__.py
@create date: 2023-12-06
@file_description：
"""
from apps.NGS_API.common.common_time import get_current_time
from apps.NGS_API.connector.db_common import *
from flask_login import current_user
import pandas as pd

class data_checker(object):
    def __init__(self,file_df,table_name):
        self.table_name = table_name
        self.file_df = file_df
    
    def _query_previous_ids(self,mapping):    
        cols = ""
        table_name = self.table_name
        for key,_ in mapping.items():
            cols = cols + key + ","
        cols = cols.rstrip(",")
        query_sql = '''
            SELECT 
                {cols}
            FROM {table_name}
        '''.format(
            cols = cols,
            table_name = table_name
        )
        base_infos = query_info(query_sql)
        return base_infos

    def remove_duplicated_data(self,mapping):
        base_infos = self._query_previous_ids(mapping)
        final_df = pd.DataFrame(data=None,columns=self.file_df.columns)
        if base_infos == None:
            pass
        else:
            infos_array = [ info[0] + "_" + info[1] for info in base_infos ]
            for _,row in self.file_df.iterrows():
                df_infos = "_".join([ row[id] for _,id in mapping.items()])
                if df_infos in infos_array:
                    pass
                else:
                    final_df.loc[len(final_df)]=row
        return final_df

    def check_id_existence(self,id,col,col_name):
        query_id_sql = '''
            SELECT 
                {col}
            FROM 
                {table_name}
            WHERE
                {col_name} = \"{id}\" 
        '''.format(
            col = col,
            table_name = self.table_name,
            col_name = col_name,
            id = id
        )
        result = query_info(query_id_sql)
        return result
        
    
class fileup_log_record(object):
    def __init__(self,row,form):
        self.row = row
        self.form = form
        self.action_type = {
            "-2":"清退",
            "1":"入库",
            "-1":"出库",
            "0":"删除"
        }

    def outstock_log(self,id):
        pass
    
    def instock_log(self):
        table_cols = ""
        table_values = '''
            \"{action_id}\",
            \"{sample_id}\",
            \"{operate_user}\",
            \"{check_user}\",
            {action_type},
            \"{operate_time}\",
            \"{create_time}\",
            \"{modify_time}\"
            '''.format(
                action_id="ACT90001",
                sample_id=self.row["样品编号"],
                operate_user=current_user.username,
                check_user=current_user.username,
                action_type=1,
                operate_time=get_current_time(),
                create_time=get_current_time(),
                modify_time=get_current_time()
        )
        for _,values in self.form["match"].items():
            table_cols = table_cols + values.split("|")[0] + ","
        table_cols = table_cols.rstrip(",")
        insert_log_sql = '''
            INSERT INTO {table_name} 
                ({table_cols})
            VALUES 
                ({table_values})
        '''.format(
            table_name = self.form["name"],
            table_cols = table_cols,
            table_values = table_values
        )
        add_info(insert_log_sql)